﻿using Mirror;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class OnlineFight : Level
{
    public Button m_BackB;

    protected void Awake()
    {
    }
    protected override void Start()
    {
        base.Start();
        if(UtilsGame.IsOnline() && !NetworkServer.active)
        {
            m_BackB.gameObject.SetActive(false);
        }
    }

    public void OnBackClicked()
    {
        if (NetworkServer.active && NetworkClient.isConnected)
        {
            (OnlineManager.singleton as OnlineManager).ReturnToRoom();
        }
        else if (NetworkClient.isConnected)
        {
            MessageShow.Instance.Messaage("等下游戏结果吧！游戏完成后主机来组织回到房间页面");
        }
        else if (NetworkServer.active)
        {
            (OnlineManager.singleton as OnlineManager).ReturnToRoom();
        }
    }
    protected override void CheckComplete()
    {
        if (!UtilsGame.IsOnline())
            return;

        var roomManager = OnlineManager.singleton as OnlineManager;
        if (!roomManager)
            return;

        bool isSucceed = true;
        for (int i = 0; i < roomManager.roomSlots.Count; ++i)
        {
            var player = roomManager.roomSlots[i] as PlayerNetwork;
            if(!player.isLocalPlayer && player.m_IsDeath == false)
            {
                isSucceed = false;
            }
            if (player.isLocalPlayer && player.m_IsDeath == true)
            {
                m_LoseItems.Add(ItemFactory.Instance.createItem(Item.ItemsType.MATERIAL_PROP_LOSE_MEDAL.ToString()));
                Player.Instance.addItem(Item.ItemsType.MATERIAL_PROP_LOSE_MEDAL);
                OnLose();
                return;
            }
        }
        if (isSucceed)
        {
            m_GetItems.Add(ItemFactory.Instance.createItem(Item.ItemsType.MATERIAL_PROP_WIN_MEDAL.ToString()));
            OnSucceed();
        }
    }
}